package com.lv.controller;


import com.lv.data.Result;
import com.lv.service.CategoryService;
import com.lv.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result getAll(){
        List<CategoryVO> categoryVOList = categoryService.getAll();
        return Result.OK().setData(categoryVOList);
    }

    @PostMapping("addCategory")
    public Result addThis(@RequestBody CategoryVO vo){
        categoryService.addThis(vo);
        return Result.OK();
    }

    @PostMapping("toUpdate")
    public Result toUpdate(@RequestParam Long id){
        CategoryVO vo = categoryService.toUpdate(id);
        return Result.OK().setData(vo);
    }

    @PostMapping("updateCategory")
    public Result updateCategory(@RequestBody CategoryVO vo){
        categoryService.updateCategory(vo);
        return Result.OK();
    }

    //删除
    @DeleteMapping
    public Result deleteBatch(@RequestParam Long[] ids){
        try {
            categoryService.deleteBatch(ids);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
}
